package com.water.bs.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.water.bs.mapper.UserManageMapper;
import com.water.bs.mapper.UserMapper;
import com.water.bs.pojo.User;
import com.water.bs.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@CrossOrigin
public class UserManageController {
    @Autowired
    private UserManageMapper userManageMapper;

    @Autowired
    private UserService userService;

    @RequestMapping("/getUserList")
    public JSON getUserList(){
        JSONObject rs = new JSONObject();
        List<User> users = userManageMapper.queryAllUser();
        System.out.println("用户列表数量:"+users.size());
        if (users.size()>0){
            rs.put("state","0");
            rs.put("message","更新成功!");
            rs.put("data",users);
            return  rs;
        }else{
            rs.put("state","1");
            rs.put("message","更新用户列表失败!");
            return  rs;
        }

    }

    @RequestMapping("/deleteUser")
    public JSON deleteUser(@Param("id")String id){
        System.out.println("获取需要删除的id:"+id);
        JSONObject rs = new JSONObject();
        User targetUser = userService.getUserById(id);
        if (targetUser.getRole() == 0){
            rs.put("state","2");
            rs.put("message","无法删除管理员!");
            return rs;
        }
        int i = userManageMapper.deleteUser(id);
        if (i>0){
            rs.put("state","0");
            rs.put("message","删除成功!");
            return  rs;
        }else{
            rs.put("state","1");
            rs.put("message","删除用户失败!");
            return  rs;
        }
    }

    @RequestMapping("/editUser")
    public JSON editUser(@RequestBody JSONObject editUserInfo){
        JSONObject rs = new JSONObject();
        Map<String,Object> map = new HashMap<>();
        System.out.println(editUserInfo);

        String editPhone = (String) editUserInfo.get("phone");
        String editEmail = (String) editUserInfo.get("email");
        if (editPhone!=null ||editPhone!=""){
            int phoneRepeat = userService.isPhoneRepeat(editPhone);
            if (phoneRepeat>0){
                rs.put("state","2");
                rs.put("message","手机号已被注册!");
                return rs;
            }
        }
        if (editEmail !=null ||editEmail!="") {
            int emailRepeat = userService.isEmailRepeat(editEmail);
            if (emailRepeat>0){
                rs.put("state","3");
                rs.put("message","邮箱号已被注册!");
                return rs;
            }
        }

        String cityStr = StringUtils.join((Collection<String>) editUserInfo.get("city"), ',').replaceAll(",",""); //join方法把数组变成str 需要一个分隔符,最后再用replace把分隔符去了
        map.put("id",editUserInfo.get("id"));
        if (editUserInfo.get("password")!=null){
            String s = (String) editUserInfo.get("password");
            map.put("password", DigestUtils.md5DigestAsHex(s.getBytes())); //md5
        }
        if (editUserInfo.get("email")!=null){
            String lowerCaseEmail = ((String) editUserInfo.get("email")).toLowerCase();  //把注册时的邮箱 变成小写再存到数据库
            map.put("email",lowerCaseEmail);
        }
        if (editUserInfo.get("phone")!=null){
            map.put("phone",editUserInfo.get("phone"));
        }
        if (editUserInfo.get("city")!=null){
            map.put("city",cityStr);
        }
        if (editUserInfo.get("gender")!=null){
            map.put("gender",editUserInfo.get("gender"));
        }
        if (editUserInfo.get("address")!=null){
            map.put("address",editUserInfo.get("address"));
        }
        if (editUserInfo.get("nickname")!=null){
            map.put("nickname",editUserInfo.get("nickname"));
        }
        System.out.println(map);
        int i = userManageMapper.updateUser(map);
        if (i ==1){
            rs.put("state","0");
            rs.put("message","修改完成!");
            return rs;
        }else{
            rs.put("state","1");
            rs.put("message","用户不存在或信息错误!");
            return rs;
        }

    }

    @RequestMapping("/addUser")
    public JSON addUser(@RequestBody JSONObject addUserInfo){
        JSONObject rs = new JSONObject();
        Map<String,Object> map = new HashMap<>();
        map = addUserInfo;
        String editPhone = (String) addUserInfo.get("phone");
        String editEmail = (String) addUserInfo.get("email");
        int phoneRepeat = userService.isPhoneRepeat(editPhone);
        if (phoneRepeat>0){
            rs.put("state","2");
            rs.put("message","手机号已被注册!");
            return rs;
        }
        int emailRepeat = userService.isEmailRepeat(editEmail);
        if (emailRepeat>0){
            rs.put("state","3");
            rs.put("message","邮箱号已被注册!");
            return rs;
        }

        map.put("id", UUID.randomUUID().toString());
        System.out.println("新用户:"+addUserInfo);
        String cityStr = StringUtils.join((Collection<String>) addUserInfo.get("city"), ',').replaceAll(",",""); //join方法把数组变成str 需要一个分隔符,最后再用replace把分隔符去了
        map.put("city",cityStr);
        map.put("role",1);
        map.put("point",0);
        map.put("shoppoint",0);
        String s = (String) addUserInfo.get("password");
        map.put("password", DigestUtils.md5DigestAsHex(s.getBytes())); //md5
        String lowerCaseEmail = ((String) addUserInfo.get("email")).toLowerCase();  //把注册时的邮箱 变成小写再存到数据库
        map.put("email",lowerCaseEmail);
        System.out.println(map);
        int i = userManageMapper.addUser(map);
        if (i ==1){
            rs.put("state","0");
            rs.put("message","添加完成!");
            return rs;
        }else{
            rs.put("state","1");
            rs.put("message","添加失败!");
            return rs;
        }

    }


}
